Skip Headers

Oracle® Database PL/SQL User's Guide and Reference 10g Release 2 (10.2) Part Number B14261-01 |
Home | Book List | Contents | Index | Master Index | Feedback

Previous | Next
| View PDF

# EXIT Statement

The `EXIT` statement breaks out of a loop. The `EXIT` statement has two forms: the unconditional `EXIT` and the conditional `EXIT` `WHEN`. With either form, you can name the loop to be exited. For more information, see "Controlling Loop Iterations: LOOP and EXIT Statements".

Syntax

exit ::=

Description of the illustration exit_statement.gif

Keyword and Parameter Description

boolean_expression

An expression that returns the Boolean value `TRUE`, `FALSE`, or `NULL`. It is evaluated with each iteration of the loop. If the expression returns `TRUE`, the current loop (or the loop labeled by `label_name`) is exited immediately. For the syntax of `boolean_expression`, see "Expression Definition".

EXIT

An unconditional `EXIT` statement (that is, one without a `WHEN` clause) exits the current loop immediately. Execution resumes with the statement following the loop.

label_name

Identifies the loop exit from: either the current loop, or any enclosing labeled loop.

Usage Notes

The `EXIT` statement can be used only inside a loop; you cannot exit from a block directly. PL/SQL lets you code an infinite loop. For example, the following loop will never terminate normally so you must use an `EXIT` statement to exit the loop.

`WHILE TRUE LOOP ... END LOOP;`

If you use an `EXIT` statement to exit a cursor `FOR` loop prematurely, the cursor is closed automatically. The cursor is also closed automatically if an exception is raised inside the loop.

Examples

For examples, see the following:

Example 4-8, "Using an EXIT Statement"
Example 4-17, "Using EXIT in a LOOP"
Example 4-18, "Using EXIT With a Label in a LOOP"

Related Topics

"Expression Definition"
"LOOP Statements"